package com.zdft.promotion.promotion.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdft.promotion.common.util.RestfulResult;
import com.zdft.promotion.promotion.bean.SingleProductBean;
import com.zdft.promotion.promotion.bean.SingleProductDetailsBean;
import com.zdft.promotion.promotion.model.PromotionFilterModel;
import com.zdft.promotion.promotion.service.api.ISingleProductDetailsService;
import com.zdft.promotion.promotion.service.api.ISingleProductService;
import com.zdft.promotion.promotion.vo.SingleProductDetailsResponseVO;
import com.zdft.promotion.promotion.vo.SingleProductDetailsVO;
import com.zdft.promotion.promotion.vo.SingleProductResponseVO;
import com.zdft.promotion.promotion.vo.SingleProductVO;
import com.zdft.promotion.sso.util.AuthenticationUtil;
import com.zdft.promotion.sso.vo.UserBaseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * ${comments}
 *
 * @author wangtao
 * @email 
 * @date 2020-02-24 16:46:46
 */
@Api(description = "单品分档控制器")
@RestController
@RequestMapping("single/product")
public class SingleProductController {
    @Autowired
    private ISingleProductService singleProductService;
    @Autowired
    private ISingleProductDetailsService singleProductDetailsService;

    @ApiOperation("通过主键查询单品分档信息")
    @GetMapping("/findById")
    public SingleProductBean findById(@RequestParam Long id){
        return singleProductService.getById(id);
    }

    @ApiOperation("查询促销流程下的单品分档")
    @GetMapping("/findAll")
    public SingleProductResponseVO findAll(@RequestBody PromotionFilterModel model){
        QueryWrapper<SingleProductBean> query = new QueryWrapper<>();
        query.lambda().eq(SingleProductBean::getPromotionRulesId, model.getId());
        Page<SingleProductBean> page = new Page(model.getCurrent(), model.getSize());
        IPage<SingleProductBean> result = singleProductService.page(page, query);
        return new SingleProductResponseVO(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords());
    }

    @ApiOperation("删除单品分档")
    @GetMapping("/delete")
    public RestfulResult delete(@RequestParam @NotNull(message = "要删除的主键不能为空") Long id){
        singleProductService.delete(id);
        return new RestfulResult();
    }

    @ApiOperation("新增/修改")
    @GetMapping("/saveOrUpdate")
    public RestfulResult save(@RequestBody SingleProductVO vo){
        UserBaseVO userBase = AuthenticationUtil.get().getUserBase();
        SingleProductBean bean = new SingleProductBean();
        BeanUtils.copyProperties(vo, bean);
        if(bean.getId() == null){
            bean.setCreateUser(userBase.getEmpCode());
            bean.setCreateTime(new Date());
        }else{
            bean.setUpdateUser(userBase.getEmpCode());
            bean.setUpdateTime(new Date());
        }
        List<SingleProductDetailsBean> details = new ArrayList<>();
        for (SingleProductDetailsVO detailVo : vo.getDetails()) {
            SingleProductDetailsBean detailsBean = new SingleProductDetailsBean();
            BeanUtils.copyProperties(detailVo, detailsBean);
            if(detailsBean.getId() == null){
                detailsBean.setCreateUser(userBase.getEmpCode());
                detailsBean.setCreateTime(new Date());
            }else{
                detailsBean.setUpdateUser(userBase.getEmpCode());
                detailsBean.setUpdateTime(new Date());
            }
            details.add(detailsBean);
        }
        singleProductService.saveOrUpdate(bean, details);
        return new RestfulResult();
    }

    @ApiOperation("通过单品分档主键查询分档明细")
    @GetMapping("/details/findAll")
    public SingleProductDetailsResponseVO findDetails(@RequestBody PromotionFilterModel model){
        QueryWrapper<SingleProductDetailsBean> query = new QueryWrapper<>();
        query.lambda().eq(SingleProductDetailsBean::getSingleProductId, model.getId());
        IPage<SingleProductDetailsBean> page = new Page<>(model.getCurrent(), model.getSize());
        IPage<SingleProductDetailsBean> result = singleProductDetailsService.page(page, query);
        return new SingleProductDetailsResponseVO(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords());
    }

    @ApiOperation("删除单品分档明细")
    @GetMapping("/details/delete")
    public RestfulResult deleteDetails(@RequestParam @NotNull(message = "要删除的明细主键不能为空") Long id){
        singleProductDetailsService.removeById(id);
        return new RestfulResult();
    }

}
